#include "KVMiddleware.h"

#include <cmath>

#include "KVCommand.h"


void KVMiddleware::processing_command(const std::string &str) {
    if (auto kv_command = dynamic_cast<KVCommand *>(command_.get())) {
        std::istringstream iss(str);
        std::vector<std::string> tokens;
        std::string token;

        // 按空格切分字符串
        while (iss >> token) {
            tokens.emplace_back(token);
        }

        if (tokens[0] == "INSERT") {
            kv_command->set_command(KVCommand::Command::INSERT);
            is_search_ = false;
        } else if (tokens[0] == "SET") {
            kv_command->set_command(KVCommand::Command::SET);
            is_search_ = false;
        } else if (tokens[0] == "DELETE") {
            kv_command->set_command(KVCommand::Command::DELETE);
            is_search_ = false;
        } else if (tokens[0] == "SEARCH") {
            is_search_ = true;
            kv_command->set_command(KVCommand::Command::SEARCH);
        } else {
            kv_command->set_command(KVCommand::Command::None);
            is_search_ = false;
        }

        kv_command->set_key(tokens[1]);
        kv_command->set_value((tokens.size() > 2) ? tokens[2] : "");
    }
}